<?php

    require_once 'User.php';
    require_once 'Connection.php';

    class Customer extends User {

        protected $Name;
        protected $Email;
        protected $BillingAddress;
        protected $DeliveryAddress;
        protected $CreditCard;

        public function __construct($username, $password, $name, $email, $billingaddress, $deliveryaddress, $creditcard, $locked) {
            $this->Username = $username;
            $this->Password = $password;
            $this->UserType = UserType::$Customer;
            $this->Locked = $locked;
            $this->Name = $name;
            $this->Email = $email;
            $this->BillingAddress = $billingaddress;
            $this->DeliveryAddress = $deliveryaddress;
            $this->CreditCard = $creditcard;
        }

        public function __get($key) {
            if(property_exists($this, $key)) {
                return $this->{$key};
            }
            else {
                return NULL;
            }
        }

        public function __set($key, $value) {
            throw new Exception('Class is immutable.');
        }

        public static function InsertCustomer($customer) {
            Connection::Connect();
            if($try = Customer::ReadCustomer($customer->Username) != NULL)
                return $try->Username;

            $nameid = FullName::InsertFullName($customer->Name);
            $billingaddressid = Address::InsertAddress($customer->BillingAddress);
            $deliveryaddressid = Address::InsertAddress($customer->DeliveryAddress);
            $creditcardid = CreditCard::InsertCreditCard($customer->CreditCard);

            $sql = mysql_query(
                "
                    INSERT INTO 
                    User (
                        Username, 
                        Password, 
                        UserType, 
                        NameId, 
                        Email, 
                        BillingAddressId, 
                        DeliveryAddressId, 
                        CreditCardNo
                    ) 
                    VALUES (
                        '$customer->Username', 
                        '$customer->Password', 
                        '".UserType::$Customer."', 
                        '$nameid', 
                        '$customer->Email', 
                        '$billingaddressid', 
                        '$deliveryaddressid', 
                        '$creditcardid'
                    )
                "
            );

            if($sql == FALSE)
                return NULL;

            return $customer->Username;
        }

        public static function ReadCustomer($customerusername) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    SELECT 
                        *
                    FROM 
                        User 
                    WHERE
                        Username = '$customerusername'
                        AND 
                        UserType = '".UserType::$Customer."' 
                        AND
                        Deleted = FALSE
                "
            );

            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    $username = $row['Username'];
                    $password = $row['Password'];
                    $nameid = $row['NameId'];
                    $email = $row['Email'];
                    $billingaddressid = $row['BillingAddressId'];
                    $deliveryaddressid = $row['DeliveryAddressId'];
                    $creditcardid = $row['CreditCardNo'];
                    $locked = $row['Locked'];

                    return new Customer(
                        $username,
                        $password,
                        FullName::ReadFullName($nameid),
                        $email,
                        Address::ReadAddress($billingaddressid),
                        Address::ReadAddress($deliveryaddressid),
                        CreditCard::ReadCreditCard($creditcardid), 
                        $locked
                    );
                }
            }
            return NULL;
        }

        public static function UpdateCustomer($customer) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        User 
                    SET 
                        Password = '$customer->Password', 
                        NameId = '".FullName::UpdateFullName($customer->Name)."', 
                        Email = '$customer->Email', 
                        BillingAddressId = '".Address::UpdateAddress($customer->BillingAddress)."', 
                        DeliveryAddressId = '".Address::UpdateAddress($customer->DeliveryAddress)."', 
                        CreditCardNo = '".CreditCard::UpdateCreditCard($customer->CreditCard)."'
                    WHERE 
                        Username = '$customer->Username'
                        AND
                        UserType = '".UserType::Customer."'' 
                        AND
                        Deleted = FALSE
                " 
            );

            if($sql == FALSE)
                return NULL;

            return $customer->Username;
        }

        public static function DeleteCustomer($customerusername) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        User 
                    SET 
                        Deleted = TRUE 
                    WHERE 
                        Username = '$customerusername'
                        AND
                        UserType = '".UserType::Customer."''
                " 
            );

            if($sql == FALSE)
                return FALSE;

            return TRUE;
        }

        public static function SearchCustomer($customer) {
            Connection::Connect();
            $where = "WHERE user.UserType = '".UserType::Customer."' AND Deleted = FALSE ";
            $conds_present = TRUE;
            if($customer->Username != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "user.Username LIKE '%$customer->Username%' ";
            }
            if($customer->Password != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "user.Password LIKE %$customer->Password%' ";
            }
            if($customer->Name != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where .
                    "
                    name.FirstName LIKE '%$customer->Name->FirstName%' 
                    AND 
                    name.LastName LIKE '%$customer->Name->LastName%' 
                    AND 
                    name.MiddleInitial LIKE '%$customer->Name->MiddleInitial%' 
                    AND 
                    name.NameId = user.NameId 
                    ";
            }
            if($customer->Email != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "user.Email LIKE '%$customer->Email%' ";
            }
            if($customer->BillingAddress != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where .
                    "
                    addr.LotNo LIKE '%$customer->BillingAddress->LotNo%' 
                    AND 
                    addr.Street LIKE '%$customer->BillingAddress->Street%' 
                    AND 
                    addr.Subdivision LIKE '%$customer->BillingAddress->Subdivision%' 
                    AND 
                    addr.City LIKE '%$customer->BillingAddress->City%' 
                    AND 
                    addr.PostalCode LIKE '%$customer->BillingAddress->PostalCode%' 
                    AND 
                    addr.Country LIKE '%$customer->BillingAddress->Country%' 
                    AND 
                    addr.AddressId = user.BillingAddressId 
                    ";
            }
            if($customer->DeliveryAddress != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where .
                    "
                    addr.LotNo LIKE '%$customer->DeliveryAddress->LotNo%' 
                    AND 
                    addr.Street LIKE '%$customer->DeliveryAddress->Street%' 
                    AND 
                    addr.Subdivision LIKE '%$customer->DeliveryAddress->Subdivision%' 
                    AND 
                    addr.City LIKE '%$customer->DeliveryAddress->City%' 
                    AND 
                    addr.PostalCode LIKE '%$customer->DeliveryAddress->PostalCode%' 
                    AND 
                    addr.Country LIKE '%$customer->DeliveryAddress->Country%' 
                    AND 
                    addr.AddressId = user.DeliveryAddressId 
                    ";
            }
            if($customer->CreditCard != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where .
                    "
                    name.FirstName LIKE '%$customer->CreditCard->FullName->FirstName%' 
                    AND 
                    name.LastName LIKE '%$customer->CreditCard->FullName->LastName%' 
                    AND 
                    name.MiddleInitial LIKE '%$customer->CreditCard->FullName->MiddleInitial%' 
                    AND 
                    name.NameId = card.NameId 
                    AND
                    card.CardNo LIKE '%$customer->CreditCard->CardNo%' 
                    AND 
                    card.Type LIKE '%$customer->CreditCard->Type%' 
                    AND 
                    YEAR(card.ExpirationDate) LIKE '%$customer->CreditCard->ExpirationDate['year']%' 
                    AND 
                    MONTH(card.ExpirationDate) LIKE '%$customer->CreditCard->ExpirationDate['mon']%' 
                    AND 
                    DAY(card.ExpirationDate) LIKE '%$customer->CreditCard->ExpirationDate['mday']%' 
                    AND 
                    card.CardNo = user.CreditCardNo 
                    ";
            }
            if(!$conds_present)
                $where = "";

            $sql = mysql_query(
                "
                    SELECT DISTINCT
                        user.*
                    FROM 
                        User user, 
                        FullName name, 
                        Address addr, 
                        CreditCard card 
                "
                .
                $where
            );

            if($sql == NULL)
                return NULL;

            $items = array();
            while($row = mysql_fetch_assoc($sql))
            {
                $username = $row['Username'];
                $password = $row['Password'];
                $nameid = $row['NameId'];
                $email = $row['Email'];
                $billingaddressid = $row['BillingAddressId'];
                $deliveryaddressid = $row['DeliveryAddressId'];
                $creditcardid = $row['CreditCardNo'];
                $locked = $row['Locked'];

                $items[] = new Customer(
                    $username,
                    $password,
                    FullName::ReadFullName($nameid),
                    $email,
                    Address::ReadAddress($billingaddressid),
                    Address::ReadAddress($deliveryaddressid),
                    CreditCard::ReadCreditCard($creditcardid), 
                    $locked
                );
            }
            return items;
        }

    }

?>